package com.situ.personal.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.personal.entity.Department;
import com.situ.personal.entity.PageInfo;
import com.situ.personal.entity.Role;
import com.situ.personal.service.RoleService;

/**
 * Servlet implementation class RoleServlet
 */
@WebServlet("/role")
public class RoleServlet extends HttpServlet {
	private RoleService roleService = new RoleService();
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//req.setCharacterEncoding("UTF-8");
		String method = req.getParameter("method");
		// 如果没有传递method让他的默认值是"findByPage"
		if (method == null || "".equals(method)) {
			method = "findByPage";
		}
		switch (method) {
		case "findByPage":
			findBypage(req, resp);
			break;
		case "findAll":
			findAll(req, resp);
			break;
		case "deleteById":
			deleteById(req, resp);
			break;
		case "insert":
			insert(req, resp);
			break;
		case "selectById":
			selectById(req, resp);
			break;
		case "update":
			update(req, resp);
			break;
		default:
			break;
		}
	}
	private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		String name = req.getParameter("name");
		String sn = req.getParameter("sn");
		
		Role role = new Role(Integer.parseInt(id), name, sn);
		System.out.println(role);
		boolean isSuccess = roleService.update(role);
		
		if (isSuccess) {
			// 修改完之后，重定向
			resp.sendRedirect(req.getContextPath() + "/role?method=findByPage");
		} else {
			resp.sendRedirect(req.getContextPath() + "/error.html");
		}
	}

	private void selectById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String selectId = req.getParameter("id");
		Role role = roleService.selectById(Integer.parseInt(selectId));
		req.setAttribute("role",role);
		req.getRequestDispatcher("/role_update.jsp").forward(req, resp);	
	}

	private void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String name = req.getParameter("name");
		String sn = req.getParameter("sn");
		
		System.out.println("错在");
		System.out.println(name);
		System.out.println(sn);
		                      
		Role role = new Role(name, sn);
		System.out.println(role);
		boolean isSuccess = roleService.insert(role);
		// 添加完之后再到查询并展示所有的界面
		// http://localhost:8080/Java1912Web/findAllStudent
		resp.sendRedirect(req.getContextPath() + "/role?method=findByPage");	
	}

	private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		// TODO Auto-generated method stub
		String id = req.getParameter("id");
		System.out.println("id:" + id);
		boolean isSuccess = roleService.deleteById(Integer.parseInt(id));
		resp.sendRedirect(req.getContextPath() + "/role?method=findByPage");
	}

	private void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List<Role> list = roleService.findAll();
		for (Role role : list) {
			System.out.println(role);
		}
		req.setAttribute("list", list);
		req.getRequestDispatcher("/role_list.jsp").forward(req, resp);	
	}

	private void findBypage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String pageNo = req.getParameter("pageNo");
		String pageSize = req.getParameter("pageSize");
		if (pageNo == null || "".equals(pageNo)) {
			pageNo = "1";
		}
		if (pageSize == null || "".equals(pageSize)) {
			pageSize = "5";
		}
		//传给service
		PageInfo pageInfo = roleService.findByPage(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
		req.setAttribute("pageInfo",pageInfo);
		req.getRequestDispatcher("/role_page_list.jsp").forward(req, resp);
		
		
	}

}



